package team.pwy.service;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.query_dsl.FuzzyQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.client.elc.QueryBuilders;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.stereotype.Service;
import team.pwy.mapper.TypeMapper;
import team.pwy.pojo.entity.Movie;
import team.pwy.pojo.entity.MovieIndex;
import team.pwy.pojo.entity.Type;
import team.pwy.pojo.entity.TypeIndex;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class TypeIndexServiceImpl implements TypeIndexService{


    private static final Logger log = LoggerFactory.getLogger(MovieIndexServiceImpl.class);

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    MovieIndexRepository contentsRepository;

    @Autowired
    ElasticsearchClient elasticsearchClient;
    public void initIndex() {
        // 处理索引结构
        IndexOperations indexOps = elasticsearchTemplate.indexOps(TypeIndex.class);
        if (indexOps.exists()) {
            boolean delFlag = indexOps.delete();
            log.info("movie_index exists，delete:{}", delFlag);
//            indexOps.createMapping(ContentsIndex.class);
            indexOps.createWithMapping();
        } else {
            log.info("movie_index not exists");
            indexOps.createWithMapping();
        }
        // 同步数据库表记录
        List<Type> typeList = typeMapper.findAll();
        if (!typeList.isEmpty()) {
            List<TypeIndex> typeIndexList = new ArrayList<>();
            typeList.forEach(contents -> {
                TypeIndex typeIndex = new TypeIndex();
                BeanUtils.copyProperties(contents, typeIndex);
                typeIndexList.add(typeIndex);
            });
            typeList.forEach(System.out::println);
            elasticsearchTemplate.save(typeIndexList);
        }
    }

    @Override
    public List<Integer> findByType(String type) throws IOException {

        System.out.println(type);
        Query byContent = FuzzyQuery.of(field -> field.field(type).value(1))._toQuery();
        SearchResponse<TypeIndex> resp = elasticsearchClient.search(
                searchReq -> searchReq.index("type_index").query(byContent),TypeIndex.class);

        System.out.println(resp);
        List<Hit<TypeIndex>> hits = resp.hits().hits();
        List<Integer> movieIdList = new ArrayList<>();
        for (Hit<TypeIndex> hit: hits) {
            TypeIndex typeIndex = hit.source();
            movieIdList.add(typeIndex.getMoveId());
            System.out.println(hit.id()+"："+typeIndex.getId());
        }
        return movieIdList;
    }
}
